package com.nf.handler;

import com.nf.MiniResultHandler;

import java.sql.ResultSet;
import java.sql.SQLException;

public class MiniScalarHandler<T> implements MiniResultHandler<T> {
    private String columName=null;
    private int columIndex=1;

    public MiniScalarHandler() {
        this(null,1);
    }

    public MiniScalarHandler(int columIndex) {
     this(null,columIndex);
    }

    public MiniScalarHandler(String columName) {
        this(columName,-1);
    }

    public MiniScalarHandler(String columName, int columIndex) {
        this.columName = columName;
        this.columIndex = columIndex;
    }

    @Override
    public T handle(ResultSet rs) throws SQLException {
        //只处理第一行记录
        if (rs.next()) {
            //总是优先以列名来获取数据
            if (columName!=null) {
                return (T) rs.getObject(columName);
            }
            return (T) rs.getObject(columIndex);
        }
        return null;
    }
}
